package fl.ass.service.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import fl.ass.facade.condition.AssChnBankAccQC;
import fl.ass.facade.dto.AssChnBankAccDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fl.ass.service.dao.AssChnBankAccMapper;
import fl.ass.service.po.AssChnBankAcc;
import fl.ass.service.service.IAssChnBankAccService;
import fl.ass.service.transform.AssChnBankAccTransform;
import cmm.service.core.framework.biz.BaseBizImpl;
import cmm.service.core.framework.dao.IBaseDao;
import cn.cmm.base.framework.exception.BaseException;


/**
 * @Description: 拖车渠道银行账户信息表 服务层接口实现
 * @Description: 类中已经实现了简单增删改查操作,可以直接使用,如果有需要重写该方法即可：
 * @Description: 1、add(TVO entity) 2、removeById(Long id) 3、modify(DTO entity) 4、selectById(Long id)	5、searchListPage(Pager page) 7、searchList(DTO dto)   
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssChnBankAccServiceImpl extends BaseBizImpl<AssChnBankAcc, AssChnBankAccDTO, AssChnBankAccQC> implements IAssChnBankAccService {
	
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(IAssChnBankAccService.class);
	
	@Autowired
	private AssChnBankAccMapper assChnBankAccMapper;
	
	@Override
	public List<AssChnBankAccDTO> searchList(AssChnBankAccQC condition) throws BaseException {
		if(null == condition) return new ArrayList<AssChnBankAccDTO>();
		List<AssChnBankAcc> list = this.assChnBankAccMapper.selectList(condition);
		return AssChnBankAccTransform.toDTOList(list);
	}
	
	@Override
	public IBaseDao<AssChnBankAcc,AssChnBankAccQC> getDao() {
		return this.assChnBankAccMapper;
	}

    @Override
    public boolean enable(Long id, Integer isValidInt) throws BaseException {
	    //如果是要启用
	    if (Objects.equals(1, isValidInt)) {
	        //先查询是否已有启用中的账户 只一个拖车渠道下可以启用一个
            AssChnBankAcc assChnBankAcc = assChnBankAccMapper.selectById(id);
            Long channelId = assChnBankAcc.getChannelId();
            AssChnBankAccQC assChnBankAccQC = new AssChnBankAccQC();
            assChnBankAccQC.setChannelId(assChnBankAcc.getChannelId());
            assChnBankAccQC.setIsDel(0);
            assChnBankAccQC.setIsValid(1);
            List<AssChnBankAcc> assChnBankAccs = assChnBankAccMapper.selectList(assChnBankAccQC);
            if (CollectionUtils.isNotEmpty(assChnBankAccs)) {
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "只能有一个启用的银行账号");
            }
        }
        return this.assChnBankAccMapper.enable(id, isValidInt);
    }

    @Override
    public AssChnBankAccDTO getByChannelId(Long id) throws BaseException {
        if(null == id){
            BaseLogUtils.newLogger("获取启用的银行账户id为空").info();
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        };
        AssChnBankAcc assChnBankAcc = assChnBankAccMapper.selectByChannelId(id);
        return AssChnBankAccTransform.toDTO(assChnBankAcc);
    }
}

